package com.baizhi.listener;

import com.alibaba.fastjson.JSON;
import com.baizhi.dao.LogDao;
import com.baizhi.entity.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import java.io.IOException;
import java.io.InputStream;

@Component
//创建一个类
public class Mqlistener {
    @Autowired(required = false)
    private LogDao logDao;

    //这个类是监听器
    //1. @JmsListener(destination = "boot-queue") destination值为目的地的名字
    //2. 方法的形参为Message message
    @JmsListener(destination = "持名法州日志")
    public void listener(Message message) throws JMSException, IOException {
        TextMessage textMessage= (TextMessage) message;
        System.out.println("正在监控"+textMessage.getText());
      //JSON.toJSONString(textMessage);
        Log log = JSON.parseObject(textMessage.getText(),Log.class);

        logDao.insert(log);
    }
}
